package com.dqhl.community.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.FormatUtils;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.view.LuocangDialog;
import com.dqhl.community.view.PictureHandlePopupWindow;
import com.dqhl.community.view.PictureSelectPopupWindow;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by guanluocang on 2016/8/29.
 * RepairsActivity
 * 我要报修
 */
public class RepairsActivity extends BaseActivity implements View.OnClickListener,OnDateSetListener {

    private ImageView iv_topBack,iv_add,iv_add_one,iv_add_two,iv_add_three,iv_add_four,iv_moreTime;
    private TextView tv_topTitle,tv_showTime;
    private Button btn_repairSubmit;//提交按钮
    private ImageView iv_repairHouse,iv_repairEnvirorment,iv_repairWater;
    private LuocangDialog dialog;
    private Context context;
    private EditText et_userName,et_userTel,et_content;
    //添加图片
    private static int REQUESTCODE_PICTURE = 100;
    private static int REQUESTCODE_CAMERA = 101;
//    private boolean isNullPicOne = true;//imageview处图片是否为空
    private PictureSelectPopupWindow pictureSelectPopupWindow;
    private PictureHandlePopupWindow pictureHandlePopupWindow;
    private String namePicOne = "",namePicTwo = "",namePicThree = "",namePicFour = "";
    private LinearLayout ll_pic_manager;
    private int countPic = 0;// 添加图片数量  imageview处图片是否为空
    private int TAG = 0;//  标记是那张图片
    //时间选择器
    TimePickerDialog mDialogMonthDayHourMinute;
    TimePickerDialog mDialogAll;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //报修选择的id
    private String houser_id = "";
    //时间的显示
    private String text = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repairs);
        context = this;
        // 实例化对象
        initView();
        // 实例化数据
        initData();
        TimePicker();
    }

    // 实例化数据
    private void initData() {
        iv_topBack.setVisibility(View.VISIBLE);
        tv_topTitle.setText(R.string.repair);
        iv_topBack.setOnClickListener(this);
        btn_repairSubmit.setOnClickListener(this);
        iv_add.setOnClickListener(this);
        iv_add_one.setOnClickListener(this);
        iv_add_two.setOnClickListener(this);
        iv_add_three.setOnClickListener(this);
        iv_add_four.setOnClickListener(this);
        iv_repairHouse.setOnClickListener(this);
        iv_repairEnvirorment.setOnClickListener(this);
        iv_repairWater.setOnClickListener(this);
//        iv_moreTime.setOnClickListener(this);
        pictureSelectPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                pictureSelectPopupWindow.backgroundAlpha(1.0f);
            }
        });
        pictureHandlePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                pictureHandlePopupWindow.backgroundAlpha(1.0f);
            }
        });
    }
    // 实例化对象
    private void initView() {
        iv_topBack = (ImageView) findViewById(R.id.iv_top_back);
        tv_topTitle = (TextView) findViewById(R.id.tv_top_title);
        btn_repairSubmit = (Button) findViewById(R.id.btn_repairSubmit);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        iv_add_one = (ImageView) findViewById(R.id.iv_add_one);
        iv_add_two = (ImageView) findViewById(R.id.iv_add_two);
        iv_add_three = (ImageView) findViewById(R.id.iv_add_three);
        iv_add_four = (ImageView) findViewById(R.id.iv_add_four);
        iv_repairHouse = (ImageView) findViewById(R.id.iv_repair_house);
        iv_repairEnvirorment = (ImageView) findViewById(R.id.iv_repair_environment);
        iv_repairWater = (ImageView) findViewById(R.id.iv_repair_waters);
//        tv_showTime = (TextView) findViewById(R.id.tv_showTime);
//        iv_moreTime = (ImageView) findViewById(R.id.iv_moreTime);
        et_userName = (EditText) findViewById(R.id.et_repair_name);
        et_userTel = (EditText) findViewById(R.id.et_repair_phone);
        et_content = (EditText) findViewById(R.id.et_manager_content);
        pictureSelectPopupWindow = new PictureSelectPopupWindow(this, pictureSelectItemsOnClick);
        pictureHandlePopupWindow = new PictureHandlePopupWindow(this, pictureHandleItemsOnClick);


    }
    View.OnClickListener pictureSelectItemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_take_photo:   // 调用系统相机
                    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(camera, REQUESTCODE_CAMERA);
                    pictureSelectPopupWindow.dismiss();
                    break;
                case R.id.btn_pick_photo:   // 调取手机相册
                    Intent picture = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(picture, REQUESTCODE_PICTURE);
                    pictureSelectPopupWindow.dismiss();
                    break;
                case R.id.btn_cancel:   // 取消
                    pictureSelectPopupWindow.dismiss();
                    break;
            }
        }
    };


    View.OnClickListener pictureHandleItemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_big_picture:
                    pictureHandlePopupWindow.dismiss();
                    Bundle bundle = new Bundle();
                    if(TAG == 1){
                        bundle.putString("img_path", namePicOne);
                        overlay(ImageShowerActivity.class, bundle);
                    }else if(TAG == 2){
                        bundle.putString("img_path", namePicTwo);
                        overlay(ImageShowerActivity.class, bundle);
                    }else if(TAG == 3){
                        bundle.putString("img_path", namePicThree);
                        overlay(ImageShowerActivity.class, bundle);
                    }else if(TAG == 4){
                        bundle.putString("img_path", namePicFour);
                        overlay(ImageShowerActivity.class, bundle);
                    }
                    break;
                case R.id.btn_delete: // 删除图片
                    pictureHandlePopupWindow.dismiss();
                    deleteImage();
                    break;
                case R.id.btn_cancel:
                    pictureHandlePopupWindow.dismiss();
                    break;
            }
        }
    };

    //控件点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.btn_repairSubmit:
                postData();
                break;
            case R.id.iv_add:
                if (countPic == 0) {
                    pictureSelectPopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                    pictureSelectPopupWindow.backgroundAlpha(0.5f);
                }else if(countPic == 1){
                    pictureSelectPopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                    pictureSelectPopupWindow.backgroundAlpha(0.5f);
                }else if(countPic == 2){
                    pictureSelectPopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                    pictureSelectPopupWindow.backgroundAlpha(0.5f);
                }else if(countPic == 3){
                    pictureSelectPopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                    pictureSelectPopupWindow.backgroundAlpha(0.5f);
                }
                break;
            case R.id.iv_add_one:
                TAG = 1;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);

                break;
            case R.id.iv_add_two:
                TAG = 2;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_add_three:
                TAG = 3;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_add_four:
                TAG = 4;
                pictureHandlePopupWindow.showAtLocation(findViewById(R.id.activity_manager), Gravity.BOTTOM, 0, 0);
                pictureHandlePopupWindow.backgroundAlpha(0.5f);
            case R.id.iv_repair_house:
                houser_id = "1";
                iv_repairHouse.setImageResource(R.drawable.ic_repair_house);
                iv_repairEnvirorment.setImageResource(R.drawable.ic_repair_enviroment_back);
                iv_repairWater.setImageResource(R.drawable.ic_repair_water_back);
                break;
            case R.id.iv_repair_environment:
                houser_id = "2";
                iv_repairHouse.setImageResource(R.drawable.ic_repair_house_back);
                iv_repairWater.setImageResource(R.drawable.ic_repair_water_back);
                iv_repairEnvirorment.setImageResource(R.drawable.ic_repair_enviroment);
                break;
            case R.id.iv_repair_waters:
                houser_id = "3";
                iv_repairWater.setImageResource(R.drawable.ic_repair_water);
                iv_repairHouse.setImageResource(R.drawable.ic_repair_house_back);
                iv_repairEnvirorment.setImageResource(R.drawable.ic_repair_enviroment_back);
                break;
//            case R.id.iv_moreTime:
//                mDialogMonthDayHourMinute.show(getSupportFragmentManager(), "month_day_hour_minute");
//                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == REQUESTCODE_PICTURE && resultCode == RESULT_OK) {
//                Uri selectedImage = data.getData();
                Uri selectedImage = geturi(data);
                String[] filePathColumns = {MediaStore.Images.Media.DATA};
                Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
                if (c != null) {
                    c.moveToFirst();
                    int columnIndex = c.getColumnIndex(filePathColumns[0]);
                    String picturePath = c.getString(columnIndex);
                    c.close();
                    //添加图片
                    addImages(picturePath);
                }

            }
            if (requestCode == REQUESTCODE_CAMERA && resultCode == RESULT_OK) {
                String sdState = Environment.getExternalStorageState();
                if (!sdState.equals(Environment.MEDIA_MOUNTED)) {
                    Dlog.e("sd card unmount");
                }
                String name = UUID.randomUUID().toString() + ".jpg";
                File file = new File("/sdcard/pintu");
                file.mkdirs();
                Bundle bundle = data.getExtras();
                //获取相机返回的数据，并转换为图片格式
                Bitmap bitmap = (Bitmap) bundle.get("data");
                FileOutputStream fout = null;
                String fileName = file.getPath() + name;
                try {
                    fout = new FileOutputStream(fileName);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fout.flush();
                        fout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(countPic == 0){
                    if ("".equals(namePicOne) && namePicOne.length()==0) {
                        namePicOne = fileName;
                        iv_add_one.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_one, namePicOne);
                        countPic += 1;
                    }
                }else if(countPic == 1){
                    if ("".equals(namePicOne)&& namePicOne.length()==0){
                        namePicOne = fileName;
                        iv_add_one.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_one, namePicOne);
                        countPic += 1;
                    } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                        namePicTwo= fileName;
                        iv_add_two.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_two, namePicTwo);
                        countPic += 1;
                    } else if ("".equals(namePicThree) && namePicThree.length()==0){
                        namePicThree= fileName;
                        iv_add_three.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_three, namePicThree);
                        iv_add.setVisibility(View.GONE);
                        countPic += 1;
                    }else if("".equals(namePicFour) && namePicFour.length()==0){
                        namePicFour= fileName;
                        iv_add_four.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_four, namePicFour);
                        countPic += 1;
                        iv_add.setVisibility(View.GONE);
                    }
                }else if(countPic == 2){
                    if ("".equals(namePicOne) && namePicOne.length()==0){
                        namePicOne = fileName;
                        iv_add_one.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_one, namePicOne);
                        countPic += 1;
                    } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                        namePicTwo= fileName;
                        iv_add_two.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_two, namePicTwo);
                        countPic += 1;
                    } else if ("".equals(namePicThree) && namePicThree.length()==0){
                        namePicThree= fileName;
                        iv_add_three.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_three, namePicThree);
                        countPic += 1;
                    }else if("".equals(namePicFour) && namePicFour.length()==0){
                        namePicFour= fileName;
                        iv_add_four.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_four, namePicFour);
                        iv_add.setVisibility(View.GONE);
                        countPic += 1;
                    }
                }else if(countPic == 3){
                    if ("".equals(namePicOne) && namePicOne.length()==0){
                        namePicOne = fileName;
                        iv_add_one.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_one, namePicOne);
                        iv_add.setVisibility(View.GONE);
                        countPic += 1;
                    } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                        namePicTwo= fileName;
                        iv_add_two.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_two, namePicTwo);
                        iv_add.setVisibility(View.GONE);
                        countPic += 1;
                    } else if ("".equals(namePicThree) && namePicThree.length()==0){
                        namePicThree= fileName;
                        iv_add_three.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_three, namePicThree);
                        iv_add.setVisibility(View.GONE);
                        countPic += 1;
                    }else if("".equals(namePicFour) && namePicFour.length()==0){
                        namePicFour= fileName;
                        iv_add_four.setVisibility(View.VISIBLE);
                        x.image().bind(iv_add_four, namePicFour);
                        countPic += 1;
                        iv_add.setVisibility(View.GONE);
                    }
                }
            }

        }
    }

    /**
     * 解决小米部分手机调用系统相册失败
     * @param intent
     * @return
     */
    public Uri geturi(android.content.Intent intent) {
        Uri uri = intent.getData();
        String type = intent.getType();
        if (uri.getScheme().equals("file") && (type.contains("image/"))) {
            String path = uri.getEncodedPath();
            if (path != null) {
                path = Uri.decode(path);
                ContentResolver cr = this.getContentResolver();
                StringBuffer buff = new StringBuffer();
                buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=")
                        .append("'" + path + "'").append(")");
                Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[] { MediaStore.Images.ImageColumns._ID },
                        buff.toString(), null, null);
                int index = 0;
                for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
                    index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
                    // set _id value
                    index = cur.getInt(index);
                }
                if (index == 0) {
                    // do nothing
                } else {
                    Uri uri_temp = Uri
                            .parse("content://media/external/images/media/"
                                    + index);
                    if (uri_temp != null) {
                        uri = uri_temp;
                    }
                }
            }
        }
        return uri;
    }
    //添加图片
    public void addImages(String path){
        if(countPic == 0){
            if ("".equals(namePicOne) && namePicOne.length()==0) {
                namePicOne = path;
                iv_add_one.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_one, namePicOne);
                countPic += 1;
            }
        }else if(countPic == 1){
            if ("".equals(namePicOne)&& namePicOne.length()==0){
                namePicOne = path;
                iv_add_one.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_one, namePicOne);
                countPic += 1;
            } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                namePicTwo= path;
                iv_add_two.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_two, namePicTwo);
                countPic += 1;
            } else if ("".equals(namePicThree) && namePicThree.length()==0){
                namePicThree= path;
                iv_add_three.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_three, namePicThree);
                iv_add.setVisibility(View.GONE);
                countPic += 1;
            }else if("".equals(namePicFour) && namePicFour.length()==0){
                namePicFour= path;
                iv_add_four.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_four, namePicFour);
                countPic += 1;
                iv_add.setVisibility(View.GONE);
            }
        }else if(countPic == 2){
            if ("".equals(namePicOne) && namePicOne.length()==0){
                namePicOne = path;
                iv_add_one.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_one, namePicOne);
                countPic += 1;
            } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                namePicTwo= path;
                iv_add_two.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_two, namePicTwo);
                countPic += 1;
            } else if ("".equals(namePicThree) && namePicThree.length()==0){
                namePicThree= path;
                iv_add_three.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_three, namePicThree);
                countPic += 1;
            }else if("".equals(namePicFour) && namePicFour.length()==0){
                namePicFour= path;
                iv_add_four.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_four, namePicFour);
                iv_add.setVisibility(View.GONE);
                countPic += 1;
            }
        }else if(countPic == 3){
            if ("".equals(namePicOne) && namePicOne.length()==0){
                namePicOne = path;
                iv_add_one.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_one, namePicOne);
                iv_add.setVisibility(View.GONE);
                countPic += 1;
            } else if ("".equals(namePicTwo) && namePicTwo.length()==0){
                namePicTwo= path;
                iv_add_two.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_two, namePicTwo);
                iv_add.setVisibility(View.GONE);
                countPic += 1;
            } else if ("".equals(namePicThree) && namePicThree.length()==0){
                namePicThree= path;
                iv_add_three.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_three, namePicThree);
                iv_add.setVisibility(View.GONE);
                countPic += 1;
            }else if("".equals(namePicFour) && namePicFour.length()==0){
                namePicFour= path;
                iv_add_four.setVisibility(View.VISIBLE);
                x.image().bind(iv_add_four, namePicFour);
                countPic += 1;
                iv_add.setVisibility(View.GONE);
            }
        }
    }
    //删除图片
    public void deleteImage(){
        if (TAG == 1){
            namePicOne = "";
            iv_add_one.setVisibility(View.GONE);
            iv_add.setVisibility(View.VISIBLE);
            iv_add.setImageResource(R.drawable.addimage);
            countPic -= 1;
        } else if(TAG == 2){
            namePicTwo = "";
            iv_add_two.setVisibility(View.GONE);
            iv_add.setVisibility(View.VISIBLE);
            iv_add.setImageResource(R.drawable.addimage);
            countPic -= 1;
        }else if(TAG == 3){
            namePicThree = "";
            iv_add_three.setVisibility(View.GONE);
            iv_add.setVisibility(View.VISIBLE);
            iv_add.setImageResource(R.drawable.addimage);
            countPic -= 1;
        }else if(TAG == 4){
            namePicFour = "";
            iv_add_four.setVisibility(View.GONE);
            iv_add.setVisibility(View.VISIBLE);
            iv_add.setImageResource(R.drawable.addimage);
            countPic -= 1;
        }
    }

    @Override
    public void onDateSet(com.jzxiang.pickerview.TimePickerDialog timePickerView, long millseconds) {
        text = getDateToString(millseconds);
        tv_showTime.setVisibility(View.VISIBLE);
        tv_showTime.setText(text);
    }
    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }
    //时间选择器
    public void TimePicker(){
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        mDialogAll = new com.jzxiang.pickerview.TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("Cancel")
                .setSureStringId("Sure")
                .setTitleStringId("TimePicker")
                .setYearText("Year")
                .setMonthText("Month")
                .setDayText("Day")
                .setHourText("Hour")
                .setMinuteText("Minute")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis())
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();


        mDialogMonthDayHourMinute = new TimePickerDialog.Builder()
                .setType(Type.MONTH_DAY_HOUR_MIN)
                .setCallBack(this)
                .build();
    }

    //提交数据
    public void postData(){
        String phone = et_userTel.getText().toString().trim();
        String name = et_userName.getText().toString().trim();
        String content = et_content.getText().toString().trim();
        if(!FormatUtils.isMobileNO(phone)){
            toast("电话格式不正确");
        }else if("".equals(name)){
            toast("姓名不能为空");
        }else if("".equals(content)){
            toast("描述不能为空");
        }else{
            File fileOne = new File(namePicOne);
            File fileTwo = new File(namePicTwo);
            File fileThree = new File(namePicThree);
            File fileFour = new File(namePicFour);
            showCustomLoadBar(R.string.loading);
            RequestParams params = new RequestParams(Config.Api.repair);
            params.addBodyParameter("phone",phone);
            params.addBodyParameter("name",name);
            params.addBodyParameter("userid",spUserInfo.getString(Constant.USERID,""));
            params.addBodyParameter("house_id",houser_id);
//            params.addBodyParameter("time",text);
//            params.addBodyParameter("time",1473393600);
            params.addBodyParameter("content",content);
            if(fileOne.isFile()){
                Dlog.e("GG报修图片一路径"+fileOne);
                params.addBodyParameter("pic_a",fileOne);
            }
            if(fileTwo.isFile()){
                Dlog.e("GG报修图片一路径"+fileTwo);
                params.addBodyParameter("pic_b",fileTwo);
            }
            if(fileThree.isFile()){
                Dlog.e("GG报修图片一路径"+fileThree);
                params.addBodyParameter("pic_c",fileThree);
            }
            if(fileFour.isFile()){
                Dlog.e("GG报修图片一路径"+fileFour);
                params.addBodyParameter("pic_d",fileFour);
            }
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getInfo(result,"errMsg");
                    String data = JsonUtils.getInfo(result,"data");
                    if(errCode == 0){
                        hideCustomLoadBar();
                        dialog = new LuocangDialog(RepairsActivity.this,"提交成功！","我们会尽快处理","确认");
                        dialog.setButtonClickListener(new LuocangDialog.ButtonClickListener() {
                            @Override
                            public void doConfirm() {
                                dialog.dismiss();
                                forward(RepairRecordActivity.class);
                            }
                        });
                        dialog.show();
                    }else{
                        hideCustomLoadBar();
                        toast("提交异常");
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Dlog.e("GG保修返回onError"+ex);
                    toast("提交异常");
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    Dlog.e("GG保修返回onCancelled");
                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                    Dlog.e("GG保修返回onFinished");
                }
            });

        }


    }
}
